import {$get, $post} from '../utils/request';
import {$msg_s, $msg_e} from '../utils/msg';

//获取房间列表
export const list = (params) => {
  let res = $get('Room/list', params);

  res = {
    data: [
      {
        roomId: '101',
        roomType: {
          roomTypeId: '1',
          roomTypeName: '单人间',
          roomTypePrice: 100,
          bedNum: 1,
        },
        roomState: {
          roomStateId: '1',
          roomStateName: '空闲',
        },
        description: '单人间描述',
      },
      {
        roomId: '102',
        roomType: {
          roomTypeId: '2',
          roomTypeName: '双人间',
          roomTypePrice: 200,
          bedNum: 2,
        },
        roomState: {
          roomStateId: '2',
          roomStateName: '维修',
        },
        description: '双人间描述',
      },
      {
        roomId: '103',
        roomType: {
          roomTypeId: '3',
          roomTypeName: '三人间',
          roomTypePrice: 300,
          bedNum: 3,
        },
        roomState: {
          roomStateId: '3',
          roomStateName: '预订',
        },
        description: '三人间描述',
      },
      {
        roomId: '104',
        roomType: {
          roomTypeId: '2',
          roomTypeName: '双人间',
          roomTypePrice: 200,
          bedNum: 2,
        },
        roomState: {
          roomStateId: '1',
          roomStateName: '空闲',
        },
        description: '双人间描述',
      },
      {
        roomId: '105',
        roomType: {
          roomTypeId: '3',
          roomTypeName: '三人间',
          roomTypePrice: 300,
          bedNum: 3,
        },
        roomState: {
          roomStateId: '1',
          roomStateName: '空闲',
        },
        description: '三人间描述',
      },
    ],
    count: 13,
    pageIndex: 1,
    pageSize: 5,
  }

  return res;
}

//验证添加入参
const checkInput = (params) => {
  if(!params.roomId){
    $msg_e('房间编号必填');
  }else if(params.roomTypeId === '0'){
    $msg_e('请选择房间类型');
  }else if(params.roomStateId === '0'){
    $msg_e('请选择房间状态');
  }else{
    //入参正确
    return true;
  }

  return false;
}

//添加房间
export const add = (params) => {
  if(!checkInput(params)){
    //入参错误
    return false;
  }

  let res = $post('Room/Add', params);

  res = {
    success: true,
    message: '添加房间成功',
  }

  if(res.success){
    //成功
    $msg_s(res.message);
  }else{
    //失败
    $msg_e(res.message);
  }

  return res.success;
}

//编辑房间
export const update = (params) => {
  if(!checkInput(params)){
    //入参错误
    return false;
  }

  let res = $post('Room/Update', params);

  res = {
    success: true,
    message: '编辑房间成功',
  }

  if(res.success){
    //成功
    $msg_s(res.message);
  }else{
    //失败
    $msg_e(res.message);
  }

  return res.success;
}

//删除房间
export const del = (params) => {
  let data = $post('Room/Delete', params);

  data = {
    success: true,
    message: '删除房间成功',
  }

  const {success, message} = data;

  if(success){
    //成功
    $msg_s(message);
  }else{
    //失败
    $msg_e(message);
  }

  return success;
}